<?php

namespace Biz\service;

use Biz\service\ShopInfo as sShopInfo;
use Biz\model\ShopBasicInfo as mShopBasicInfo;
use Biz\model\ShopInvoiceInformation as mShopInvoiceInformation;
use Biz\validate\ShopInvoiceInformation as vShopInvoiceInformation;
/**
 * @deprecated
 */
class ShopInvoiceInfo
{
    public static function mgtGetInvoiceInfo($shopId)
    {
        $shopInfo = sShopInfo::getShopInfo($shopId, ['info']);
        if (!$shopInfo) {
            return [];
        }
        $info['has_info'] = false;
        if ($shopInfo['type'] == mShopBasicInfo::TYPE_COMPANY) {
            $info['is_company'] = true;
            $info['name'] = $shopInfo['info']['name'];
            $info['social_credit_code'] = $shopInfo['info']['social_credit_code'];
        } else {
            $info['is_company'] = false;
            $info['name'] = $shopInfo['info']['realname'];
        }
        $invoiceInfo = mShopInvoiceInformation::getInvoiceInfo($shopId);
        if ($invoiceInfo) {
            $info['has_info'] = true;
            $info = array_merge($info, $invoiceInfo);
        }
        return $info;
    }

    public static function mgtAddInvoiceInfo($shopId, $jsonData)
    {
        $data = json_decode($jsonData, true);
        if ($data) {
            $shopInfo = sShopInfo::getShopInfo($shopId, ['info']);
            if (!$shopInfo) {
                return false;
            }
            if ($shopInfo['type'] == mShopBasicInfo::TYPE_COMPANY) {
                $data['name'] = $shopInfo['info']['name'];
                $data['social_credit_code'] = $shopInfo['info']['social_credit_code'];
            } else {
                $data['name'] = $shopInfo['info']['realname'];
            }
            $data['send_address_code'] = (implode(',', $data['send_address_code']) !== false) ? implode(',', $data['send_address_code']) : '';
            if (in_array($data['type'], [mShopInvoiceInformation::TYPE_PERSONAL, mShopInvoiceInformation::TYPE_GENERAL, mShopInvoiceInformation::TYPE_SMALL])) {
                switch ($data['type']) {
                    case mShopInvoiceInformation::TYPE_PERSONAL:
                        $scenes = 'addPersonal';
                        break;
                    case mShopInvoiceInformation::TYPE_GENERAL:
                        $scenes = 'addGeneral';
                        break;
                    case mShopInvoiceInformation::TYPE_SMALL:
                        $scenes = 'addSmall';
                        break;
                }
                if (!vShopInvoiceInformation::validate($data, $scenes)) {
                    $msg = vShopInvoiceInformation::firstError();
                    throw new \Exception($msg);
                }
                $addData = [
                    'shop_id' => $shopId,
                    'type' => $data['type'],
                    'name' => $data['name'],
                    'social_credit_code' => isset($data['social_credit_code']) ? $data['social_credit_code'] : '',
                    'taxpayer_identification_number' => isset($data['taxpayer_identification_number']) ? $data['taxpayer_identification_number'] : '',
                    'bank_name' => isset($data['bank_name']) ? $data['bank_name'] : '',
                    'bank_no' => isset($data['bank_no']) ? $data['bank_no'] : '',
                    'company_address' => isset($data['company_address']) ? $data['company_address'] : '',
                    'company_phone' => isset($data['company_phone']) ? $data['company_phone'] : '',
                    'send_address_code' => $data['send_address_code'],
                    'send_address' => $data['send_address'],
                    'consignee_name' => $data['consignee_name'],
                    'consignee_phone' => $data['consignee_phone'],
                ];
                return mShopInvoiceInformation::addData($addData);
            } else {
                throw new \Exception('请选择开票类型');
            }
        } else {
            throw new \Exception('请求错误');
        }


    }


    public static function mgtChangeInvoiceSendInfo($shopId, $jsonData)
    {
        if (!mShopInvoiceInformation::hasInvoiceInfo($shopId)) {
            throw new \Exception('未设置过发票信息');
        }
        $data = json_decode($jsonData, true);
        if ($data) {
            $data['send_address_code'] = (isset($data['send_address_code']) && implode(',', $data['send_address_code']) !== false) ? implode(',', $data['send_address_code']) : '';
            if (!vShopInvoiceInformation::validate($data, 'changeSend')) {
                $msg = vShopInvoiceInformation::firstError();
                throw new \Exception($msg);
            }
            $changeData = [
                'send_address_code' => $data['send_address_code'],
                'send_address' => $data['send_address'],
                'consignee_name' => $data['consignee_name'],
                'consignee_phone' => $data['consignee_phone'],
            ];
            return mShopInvoiceInformation::updateData($changeData, ['shop_id' => $shopId]);
        } else {
            throw new \Exception('请求错误');
        }

    }

    public static function hasInvoiceInfo($shopId)
    {
        return mShopInvoiceInformation::hasInvoiceInfo($shopId);
    }

    public static function admGetInvoiceInfo($shopId)
    {
        $shopInfo = sShopInfo::getShopInfo($shopId, ['info']);
        if (!$shopInfo) {
            return [];
        }
        $info['has_info'] = false;
        if ($shopInfo['type'] == mShopBasicInfo::TYPE_COMPANY) {
            $info['is_company'] = true;
            $info['name'] = $shopInfo['info']['name'];
            $info['social_credit_code'] = $shopInfo['info']['social_credit_code'];
        } else {
            $info['is_company'] = false;
            $info['name'] = $shopInfo['info']['realname'];
        }
        $invoiceInfo = mShopInvoiceInformation::getInvoiceInfo($shopId, false);
        if ($invoiceInfo) {
            $info['has_info'] = true;
            $info = array_merge($info, $invoiceInfo);
        }
        return $info;
    }
}